﻿using Library.GeneralUtil;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace Windows8AppsExample
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class DemoJsonHelper : Windows8AppsExample.Common.LayoutAwarePage
    {
        public DemoJsonHelper()
        {
            this.InitializeComponent();
        }

        private class Product
        {
            public string Name { get; set; }
            public DateTime Expiry { get; set; }
            public string Price { get; set; }
            public string[] Sizes { get; set; }
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void btnConvertModelToJsonString_Click(object sender, RoutedEventArgs e)
        {
            List<Product> lstProduct = new List<Product>();

            Product product1 = new Product();
            product1.Name = "Apple";
            product1.Expiry = new DateTime(2008, 12, 28);
            product1.Price = "3.99M";
            product1.Sizes = new string[] { "Small", "Medium", "Large" };

            Product product2 = new Product();
            product2.Name = "Lement";
            product2.Expiry = new DateTime(2012, 10, 30);
            product2.Price = "4.00M";
            product2.Sizes = new string[] { "Small", "Medium", "Large", "Small", "Medium", "Large" };

            lstProduct.Add(product1);
            lstProduct.Add(product2);

            txtConvertModelToJsonString.Text = JsonHelper.ConvertObjectToJsonString(lstProduct[0]);
        }

        private void btnConvertJsonStringToModel_Click(object sender, RoutedEventArgs e)
        {
            if (txtConvertModelToJsonString.Text != "")
            {
                string resStr = "";
                JObject o = JObject.Parse(txtConvertModelToJsonString.Text);

                resStr += "Name: " + (string)o["Name"] + " - ";
                resStr += "Expiry: " +(string)o["Expiry"] + " - ";
                resStr += "Price: " + (string)o["Price"] + " - ";

                JArray sizes = (JArray)o["Sizes"];
                resStr += "Sizes: " + (string)sizes[0] + ", " + (string)sizes[1] + ", " + (string)sizes[2];
                txtConvertJsonStringToModel.Text = resStr;
            }
        }
    }
}
